package com.xbongbong.saas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 推送指标设置枚举
 *
 * @author 李杰
 * @version v1.0
 * @date 2019/2/18 19:27
 * @since v1.0
 */
@Getter
public enum ReportTargetEnum {

    //RedundantTemplateTypeEnum：1客户 2机会 3合同 5产品 6跟进记录 11回款单 12应收款 13已付款 14未付款 101采购合同 103入库单 104出库单 120发票
    //XbbRefTypeEnum：1合同 2客户 5机会 6跟进记录 7产品 11回款单 13应收款 14已付款 15未付款 90采购合同 103入库单 104出库单 16发票
    COUNT_CUSTOMER(1, "countCustomer", "enum.reportTarget.name.count_customer", 9999, 2, "enum.reportTarget.unit.count_customer", XbbRefTypeEnum.CUSTOMER.getCode(), "|" + XbbRefTypeEnum.CUSTOMER.getCode() + "|", 1, SystemCodeEnum.CUSTOMER_ADD_HOME_BOARD),
    COUNT_CUSTOMER_CON(2, "countCustomerCon", "enum.reportTarget.name.count_customer_con", 9989, 2, "enum.reportTarget.unit.count_customer_con", XbbRefTypeEnum.CUSTOMER.getCode(), "|" + XbbRefTypeEnum.CONTRACT.getCode() + "|" + XbbRefTypeEnum.CUSTOMER.getCode() + "|", 1, SystemCodeEnum.CUSTOMER_OK_HOME_COUNT),
    COUNT_CUSTOMER_OPP(3, "countCustomerOpp", "enum.reportTarget.name.count_customer_opp", 9979, 2, "enum.reportTarget.unit.count_customer_opp", XbbRefTypeEnum.CUSTOMER.getCode(), "|" + XbbRefTypeEnum.SALES_OPPORTUNITY.getCode() + "|" + XbbRefTypeEnum.CUSTOMER.getCode() + "|", 1, SystemCodeEnum.OPPORTUNITY_CUSTOMER_BOARD),
    COUNT_FOLLOW_CUSTOMER(4, "countFollowCustomer", "enum.reportTarget.name.count_follow_customer", 9969, 2, "enum.reportTarget.unit.count_follow_customer", XbbRefTypeEnum.CUSTOMER.getCode(), "|" + XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode() + "|" + XbbRefTypeEnum.CUSTOMER.getCode() + "|", 1, SystemCodeEnum.CUSTOMER_FOLLOW_BOARD),
    COUNT_COMMUNICATE(5, "countCommunicate", "enum.reportTarget.name.count_communicate", 9959, 2, "enum.reportTarget.unit.count_communicate", XbbRefTypeEnum.CUSTOMER.getCode(), "|" + XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode() + "|", 1, SystemCodeEnum.COMMUNICATE_ADD_BOARD),
    COUNT_CONTRACT(6, "countContract", "enum.reportTarget.name.count_contract", 9949, 2, "enum.reportTarget.unit.count_contract", XbbRefTypeEnum.CONTRACT.getCode(), "|" + XbbRefTypeEnum.CONTRACT.getCode() + "|", 1, SystemCodeEnum.CONTRACT_COUNT_BOARD),
    AMOUNT_CONTRACT(7, "amountContract", "enum.reportTarget.name.amount_contract", 9939, 1, "enum.reportTarget.unit.amount_contract", XbbRefTypeEnum.CONTRACT.getCode(), "|" + XbbRefTypeEnum.CONTRACT.getCode() + "|", 1, SystemCodeEnum.CONTRACT_AMOUNT_BOARD),
    COUNT_OPPORTUNITY_ADD(8, "countOpportunityAdd", "enum.reportTarget.name.count_opportunity_add", 9929, 2, "enum.reportTarget.unit.count_opportunity_add", XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), "|" + XbbRefTypeEnum.SALES_OPPORTUNITY.getCode() + "|", 1, SystemCodeEnum.OPPORTUNITY_ADD_COUNT_BOARD),
    AMOUNT_OPPORTUNITY_ADD(9, "amountOpportunityAdd", "enum.reportTarget.name.amount_opportunity_add", 9919, 1, "enum.reportTarget.unit.amount_opportunity_add", XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), "|" + XbbRefTypeEnum.SALES_OPPORTUNITY.getCode() + "|", 1, SystemCodeEnum.OPPORTUNITY_ADD_AMOUNT_BOARD),
    COUNT_OPPORTUNITY_WIN(10, "countOpportunityWin", "enum.reportTarget.name.count_opportunity_win", 9909, 2, "enum.reportTarget.unit.count_opportunity_win", XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), "|" + XbbRefTypeEnum.SALES_OPPORTUNITY.getCode() + "|", 1, SystemCodeEnum.OPPORTUNITY_WIN_COUNT),
    AMOUNT_OPPORTUNITY_WIN(11, "amountOpportunityWin", "enum.reportTarget.name.amount_opportunity_win", 9899, 1, "enum.reportTarget.unit.amount_opportunity_win", XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), "|" + XbbRefTypeEnum.SALES_OPPORTUNITY.getCode() + "|", 1, SystemCodeEnum.OPPORTUNITY_WIN_AMOUNT),
    COUNT_FOLLOW_OPPORTUNITY(12, "countFollowOpportunity", "enum.reportTarget.name.count_follow_opportunity", 9889, 2, "enum.reportTarget.unit.count_follow_opportunity", XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), "|" + XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode() + "|" + XbbRefTypeEnum.SALES_OPPORTUNITY.getCode() + "|", 1, SystemCodeEnum.OPPORTUNITY_FOLLOW_BOARD),
    AMOUNT_PAYMENT_ED(13, "amountPaymentEd", "enum.reportTarget.name.amount_payment_ed", 9879, 1, "enum.reportTarget.unit.amount_payment_ed", XbbRefTypeEnum.PAYMENT.getCode(), "|" + XbbRefTypeEnum.PAYMENT_SHEET.getCode() + "|", 1, SystemCodeEnum.PAYMENT_ED_BOARD),
    AMOUNT_PAYMENT_UN(14, "amountPaymentUn", "enum.reportTarget.name.amount_payment_un", 9869, 1, "enum.reportTarget.unit.amount_payment_un", XbbRefTypeEnum.PAYMENT.getCode(), "|" + XbbRefTypeEnum.CONTRACT.getCode() + "|" + XbbRefTypeEnum.PAYMENT.getCode() + "|", 1, SystemCodeEnum.PAYMENT_UN_BOARD),
    AMOUNT_PAY_ED(15, "amountPayEd", "enum.reportTarget.name.amount_pay_ed", 9859, 1, "enum.reportTarget.unit.amount_pay_ed", XbbRefTypeEnum.PAYMENT.getCode(), "|" + XbbRefTypeEnum.PAY_PLAN.getCode() + "|", 2, SystemCodeEnum.PAY_ED_HOME),
    AMOUNT_PAY_UN(16, "amountPayUn", "enum.reportTarget.name.amount_pay_un", 9849, 1, "enum.reportTarget.unit.amount_pay_un", XbbRefTypeEnum.PAYMENT.getCode(), "|" + XbbRefTypeEnum.PAY_PLAN.getCode() + "|", 2, SystemCodeEnum.PAY_UN_BOARD),
    AMOUNT_INVOICE(17, "amountInvoice", "enum.reportTarget.name.amount_invoice", 9839, 1, "enum.reportTarget.unit.amount_invoice", XbbRefTypeEnum.PAYMENT.getCode(), "|" + XbbRefTypeEnum.INVOICE.getCode() + "|", 2, SystemCodeEnum.INVOICE_ED_CHART),
    COUNT_PURCHASE(18, "countPurchase", "enum.reportTarget.name.count_purchase", 9829, 2, "enum.reportTarget.unit.count_purchase", XbbRefTypeEnum.PURCHASE.getCode(), "|" + XbbRefTypeEnum.PURCHASE.getCode() + "|", 2, SystemCodeEnum.PURCHASE_COUNT_BOARD),
    AMOUNT_PURCHASE(19, "amountPurchase", "enum.reportTarget.name.amount_purchase", 9819, 1, "enum.reportTarget.unit.amount_purchase", XbbRefTypeEnum.PURCHASE.getCode(), "|" + XbbRefTypeEnum.PURCHASE.getCode() + "|", 2, SystemCodeEnum.PURCHASE_AMOUNT_BOARD),
    COUNT_INSTOCK(20, "countInstock", "enum.reportTarget.name.count_instock", 9809, 1, "enum.reportTarget.unit.count_instock", XbbRefTypeEnum.INSTOCK.getCode(), "|" + XbbRefTypeEnum.INSTOCK.getCode() + "|", 2, SystemCodeEnum.IN_STOCK_PRODUCT_BOARD),
    COUNT_OUTSTOCK(21, "countOutstock", "enum.reportTarget.name.count_outstock", 9799, 1, "enum.reportTarget.unit.count_outstock", XbbRefTypeEnum.INSTOCK.getCode(), "|" + XbbRefTypeEnum.OUTSTOCK.getCode() + "|", 2, SystemCodeEnum.OUT_STOCK_PRODUCT_BOARD),
    COUNT_CLUE(22, "nowClue", "enum.reportTarget.name.count_clue", 9899, 2, "enum.reportTarget.unit.count_clue", XbbRefTypeEnum.CLUE.getCode(), "|" + XbbRefTypeEnum.CLUE.getCode() + "|", 2, SystemCodeEnum.CLUE_TOTAL_BOARD),
    ADD_CLUE(23, "addClue", "enum.reportTarget.name.add_clue", 9900, 2, "enum.reportTarget.unit.count_clue", XbbRefTypeEnum.CLUE.getCode(), "|" + XbbRefTypeEnum.CLUE.getCode() + "|", 2, SystemCodeEnum.CLUE_ADD_BOARD),
    FOLLOW_CLUE(24, "followClue", "enum.reportTarget.name.follow_clue", 9901, 2, "enum.reportTarget.unit.count_clue", XbbRefTypeEnum.CLUE.getCode(), "|" + XbbRefTypeEnum.CLUE.getCode() + "|", 2, SystemCodeEnum.CLUE_FOLLOW_BOARD);

    /**
     * 枚举值 --
     */
    private final int code;

    /**
     * 枚举信息--英文名称 -- 与targetLogEntity内指标属性名一致
     */
    private final String alias;

    /**
     * 枚举信息--中文名称
     */
    private final String name;

    /**
     * 枚举排序--越大越靠前
     */
    private final Integer sort;

    /**
     * 枚举值类型--1 Double，2 Integer
     */
    private final Integer type;

    /**
     * 枚举值类型--单位：元、个
     */
    private final String unit;

    //RedundantTemplateTypeEnum：1客户 2机会 3合同 5产品 6跟进记录 11回款单 12应收款 13已付款 14未付款 101采购合同 103入库单 104出库单 120发票
    //在页面上是在客户模块、or合同模块等等
    /**
     * 枚举值类型--属于哪一个模块
     */
    private final Integer belong;
    /**
     * 枚举值类型--关联哪一张表，例|6|1|代表关联了客户和跟进记录
     */
    private final String relateTable;

    /**
     * 枚举值类型--1 crm，2 jxc
     */
    private final Integer crmOrJxc;

    /**
     * 对应的系统指标
     */
    private final SystemCodeEnum systemCodeEnum;

    /**
     * 构造一个<code>TargetEnum</code>枚举对象
     *
     * @param code
     * @param name
     */
    ReportTargetEnum(int code, String alias, String name, int sort, int type, String unit, int belong, String relateTable, int crmOrJxc, SystemCodeEnum systemCodeEnum) {
        this.code = code;
        this.alias = alias;
        this.name = name;
        this.sort = sort;
        this.type = type;
        this.unit = unit;
        this.belong = belong;
        this.relateTable = relateTable;
        this.crmOrJxc = crmOrJxc;
        this.systemCodeEnum = systemCodeEnum;
    }

    /**
     * 通过枚举<code>code</code>获得枚举
     *
     * @return TargetEnum
     */
    public static ReportTargetEnum getByCode(Integer code) {
        for (ReportTargetEnum cacheCode : values()) {
            if (code.equals(cacheCode.getCode())) {
                return cacheCode;
            }
        }
        return null;
    }
    /**
     * 通过枚举<systemCodeEnum>systemCodeEnum</systemCodeEnum>获得枚举
     *
     * @return ReportTargetEnum
     */
    public static ReportTargetEnum getBySystemCodeEnum(SystemCodeEnum systemCodeEnum) {
        for (ReportTargetEnum cacheCode : values()) {
            if (cacheCode.systemCodeEnum == systemCodeEnum) {
                return cacheCode;
            }
        }
        return null;
    }

    /**
     * 根据指标code获取指标名称
     *
     * @param code
     * @return
     * @version
     * @author cp.zhang
     * 创建时间：2017-8-2 下午5:19:26
     * 修改时间：
     * @since
     */
    public static String getNameByCode(Integer code) {
        String name = "";
        for (ReportTargetEnum cache : values()) {
            if (code.equals(cache.getCode())) {
                name = cache.getName();
            }
        }
        return name;
    }

    /**
     * 获取全部枚举
     *
     * @return List<TargetEnum>
     */
    public static List<ReportTargetEnum> getAllEnum() {
        List<ReportTargetEnum> list = new ArrayList<>();
        for (ReportTargetEnum cache : values()) {
            list.add(cache);
        }
        return list;
    }

    /**
     * 获取全部枚举code
     *
     * @return
     */
    public static List<Integer> getAllEnumCode() {
        List<Integer> list = new ArrayList<>();
        for (ReportTargetEnum cache : values()) {
            list.add(cache.getCode());
        }
        return list;
    }

    /**
     * 获取所属模块
     *
     * @param belong
     * @return
     * @version
     * @author cp.zhang
     * 创建时间：2017-8-7 下午2:37:01
     * 修改时间：
     * @since
     */
    public static String getBelongName(Integer belong) {
        String belongName = "";
        if (belong == null) {
            return belongName;
        }
        if (belong.equals(XbbRefTypeEnum.CUSTOMER.getCode())) {
            belongName = "客户数据";
        } else if (belong.equals(XbbRefTypeEnum.CONTRACT.getCode())) {
            belongName = "合同数据";
        } else if (belong.equals(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())) {
            belongName = "机会数据";
        } else if (belong.equals(XbbRefTypeEnum.PAYMENT.getCode())) {
            belongName = "资金数据";
        } else if (belong.equals(XbbRefTypeEnum.PURCHASE.getCode())) {
            belongName = "采购数据";
        } else if (belong.equals(XbbRefTypeEnum.INSTOCK.getCode()) || belong.equals(XbbRefTypeEnum.OUTSTOCK.getCode())) {
            belongName = "仓库数据";
        } else if (belong.equals(XbbRefTypeEnum.CLUE.getCode())){
            belongName = "销售线索";
        }
        return belongName;
    }

    /**
     * 获取所有指标的index，并格式化为|1|2|3|4|... 的格式
     * 主要初始化CompanyConfigEntity表的指标配置时用到
     *
     * @return
     */
    public static String getAllIndex2ConfigValue() {
        StringBuilder configValue = new StringBuilder("|");
        for (ReportTargetEnum cache : values()) {
            Integer code = cache.code;
            configValue.append(code).append("|");
        }
        return configValue.toString();
    }

    public static Map<String, List<ReportTargetEnum>> getTopEnumList(String configValue) {
        //属于哪一个模块--该模块内的指标
        Map<Integer, List<ReportTargetEnum>> belongEnumMap = new HashMap<>(16);
        int top = 3;
        for (ReportTargetEnum cache : values()) {
            //非vip只有crm相关，排除jxc
            if (cache.crmOrJxc == 2) {
                continue;
            }
            Integer belong = cache.belong;

            List<ReportTargetEnum> enumList = belongEnumMap.get(belong);
            if (enumList == null) {
                enumList = new ArrayList<>();
            }else if (enumList.size() >= top) {
                //推送只推每个模块前top个指标
                continue;
            }
            enumList.add(cache);

            belongEnumMap.put(belong, enumList);
        }

        //模块名--该模块内的指标
        Map<String, List<ReportTargetEnum>> nameEnumMap = new LinkedHashMap<>();
        for (Map.Entry<Integer, List<ReportTargetEnum>> entry : belongEnumMap.entrySet()) {
            Integer belong = entry.getKey();
            nameEnumMap.put(getBelongName(belong), entry.getValue());
        }
        return nameEnumMap;
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }

    public String getUnit() {
        return I18nMessageUtil.getMessage(unit);
    }
}
